﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections;
using Air_condition.GUI;
using ColdStorage.GUI;
using ColdStorage.DOL;
using ThermalEquipmentDesign.View.Condenser;
using ThermalEquipmentDesign.View.CoolingTower;
using ThermalEquipmentDesign.View;
using ThermalEquipmentDesign.ViewModel;
using Com;
using Air_condition.DO;
using System.Threading;
using System.Windows.Media.Animation;
using Com.BU;
using Mainform;
using ThermalEquipmentDesign.View.CasingPipeSprinklers;
using ThermalEquipmentDesign.View.CasingPipeWing;
using ThermalEquipmentDesign.View.CoolerWet;
using ThermalEquipmentDesign.View.PipeCasingHorizontal;
using ThermalEquipmentDesign.View.SmoothPipeR717R22;
using System.Xml;
using System.Reflection;
using Microsoft.Win32;

namespace Ecosystem
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainGui : Window
    {
        ucAirConditioning MainAir;
        ucMainStorageCold MainCold;
        TemplateManagement temma;
        Storyboard animatedButtonStoryboard = new Storyboard();

        //Cooling Tower
        ucCoolingTower MainCooling;

        //  Condenser
        ucCoolAirR22 MainCoolAirR22;
        ucEvaporaterR717 MainEvaporaterR717;
        ucHorizontalPipeR12 MainHorizontalPipeR12;
        ucSprinklerR717 MainSprinklerR717;
        ucVerticalPipeR717 MainVerticalPipeR717;

        //  Evaporator
        ucCasingPipeSprinklers MainCasingPipeSprinklers;
        ucCasingPipeWing MainCasingPipeWing;
        ucCoolerWet MainCoolerWet;
        ucPlateAmoniac MainPlateAmoniac;
        SmoothPipeR717R22 MainSmoothPipeR717R22;

        object oCurrentObject;

        public MainGui()
        {
            Thread thread = new Thread(new ThreadStart(startloading));
            thread.SetApartmentState(ApartmentState.STA);
            thread.IsBackground = true;
            thread.Start();

            this.InitializeComponent();

            LoadDatabase();

            thread.Abort();

            StartPage();

        }

        private void StartPage()
        {
            ucStartPage startPage = new ucStartPage();
            startPage.MyAir = new ucStartPage.AirClick(AirClick);
            startPage.MyCold = new ucStartPage.ColdClick(ColdClick);
            startPage.MyCooling = new ucStartPage.CoolingClick(CoolingClick);
            startPage.MyCon = new ucStartPage.CondenserClick(CondenserClick);
            startPage.MyEva = new ucStartPage.EvaporatorClick(EvaporatorClick);
            ucLeftStartPage leftPage = new ucLeftStartPage();
            pnlMainGui.Children.Clear();
            pnlTreeView.Children.Clear();
            btnReport.Visibility = Visibility.Hidden;
            btnLibrary.Visibility = Visibility.Hidden;
            btnTemplate.Visibility = Visibility.Hidden;
            pnlMainGui.Children.Add(startPage);
            pnlTreeView.Children.Add(leftPage);
            mSave.Visibility = Visibility.Hidden;
        }

        public void startloading()
        {
            Loading.MainWindow loading = new Loading.MainWindow();
            loading.ShowDialog();
            System.Windows.Threading.Dispatcher.Run();
        }

        #region Load Database
        private void LoadDatabase()
        {
            LibraryManagerInCom.loadLocation();
            LibraryManagerInCom.loadHeatByMan();
            LibraryManagerInCom.loadExitingAir();
            LibraryManagerInCom.loadAirConditionPlace();
            LibraryManagerInCom.loadEquipment();
            LibraryManagerInCom.loadTypeOfGlass();
            LibraryManagerInCom.loadDirectionOfGlass();
            LibraryManagerInCom.loadWallMaterial();
            LibraryManagerInCom.loadCeilling();
            LibraryManagerInCom.loadCeillingLayer();
            LibraryManagerInCom.loadStructureRoof();
            LibraryManagerInCom.loadCharacteristicRoof();
            LibraryManagerInCom.loadRoofSurface();
            LibraryManagerInCom.loadCeilingCoefficient();
            LibraryManagerInCom.loadSolarByRadiation();
            LibraryManagerInCom.loadImmediateImpact();
            LibraryManagerInCom.loadGlassList();
            LibraryManagerInCom.loadGlassDirection();
            LibraryManagerInCom.loadWoodGlassCoefficient();
            LibraryManagerInCom.loadWindowSkywellCoefficient();
            LibraryManagerInCom.loadSurfaceCoefficient();
            LibraryManagerInCom.loadRoofCeilingCharacteristic();
            LibraryManagerInCom.loadRoofCeilingStructure();
            LibraryManagerInCom.loadRoofCeilingCoefficient();

            LibraryManagerInCom.loadRoomType();
            LibraryManagerInCom.loadLoadingStandardList();
            LibraryManagerInCom.loadProduct();
            LibraryManagerInCom.loadEnthapyList();
            LibraryManagerInCom.loadWrapping();
            LibraryManagerInCom.loadRespirationList();
            LibraryManagerInCom.loadroofType();
            LibraryManagerInCom.loadWallType();
            LibraryManagerInCom.loadCSWallMaterialList();
            LibraryManagerInCom.loadDirection();
            LibraryManagerInCom.loadColdStorageWallList();
            LibraryManagerInCom.loadTemperatureExcessList();

            LibraryManagerInCom.loadWaterCoefficientsList();
            LibraryManagerInCom.loadAirCoefficientsList();
        }
        #endregion

        #region PROCESS ALTERNATIVE
        public void GetAlt(Alternative alt)
        {
            MainAir.loadAlt(alt);
        }

        public void AddAlt(Alternative alt)
        {
            MainAir.addAlt(alt);
        }

        public void DeleteAlt()
        {
            MainAir.deleteAlt();
        }

        public void GetLocation(string sOutsideTemperaturee)
        {
            if (oCurrentObject is ucAirConditioning)
            {
                MainAir.SetOutsideTemperature(sOutsideTemperaturee);
            }
            else
            {
                if (oCurrentObject is ucMainStorageCold)
                {
                    MainCold.getMaxTemperature(sOutsideTemperaturee);
                }
            }
        }
        #endregion

        #region PROCESS ROOM
        public void GetRoom(object o)
        {
            if (oCurrentObject is ucAirConditioning)
            {
                Air_condition.DO.Room room = (Air_condition.DO.Room)o;
                MainAir.loadRoom(room);
            }
            else
            {
                if (oCurrentObject is ucMainStorageCold)
                {
                    ColdStorage.DOL.Room room = (ColdStorage.DOL.Room)o;
                    MainCold.loadroom(room);
                }
            }
        }
        //
        public void AddRoom(object o, bool update)
        {

            if (update)
            {
                ucListInformation1.updateRoom();
            }
            else
            {
                if (oCurrentObject is ucAirConditioning)
                {
                    Air_condition.DO.Room r = (Air_condition.DO.Room)o;
                    ucListInformation1.addRoom(r);
                }
                else
                {
                    if (oCurrentObject is ucMainStorageCold)
                    {
                        ColdStorage.DOL.Room r = (ColdStorage.DOL.Room)o;
                        ucListInformation1.addRoom(r);
                    }
                }
            }
        }
        //
        public void DeleteRoom()
        {
            ucListInformation1.deleteRoom();
        }
        #endregion

        public void loadTemplate(object o)
        {
            MainAir.LoadTemplate(o);
        }

        public void GetCold(Cold_Storage cold)
        {
            MainCold.getColdStorageInfo(cold);
        }

        #region Process Buttons
        #region Menu Button
        private void iHome_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            StartPage();
        }

        private void btnLibrary_Click(object sender, RoutedEventArgs e)
        {
            Com.GUI.LibraryManager.LibraryMainGUI frmL = new Com.GUI.LibraryManager.LibraryMainGUI();
            if (oCurrentObject is ucAirConditioning)
            {
                frmL.SelectTab(1);
            }
            else
            {
                if (oCurrentObject is ucMainStorageCold)
                {
                    frmL.SelectTab(2);
                }
                else
                {
                    if (oCurrentObject is ucCoolAirR22 || oCurrentObject is ucEvaporaterR717 ||
                        oCurrentObject is ucHorizontalPipeR12 || oCurrentObject is ucSprinklerR717 ||
                        oCurrentObject is ucVerticalPipeR717)
                    {
                        frmL.SelectTab(3);
                    }
                    else
                    {
                        frmL.SelectTab(3);
                    }
                }
            }
            frmL.Show();
        }

        private void btnTemplate_Click(object sender, RoutedEventArgs e)
        {
            temma.Show();
        }

        #region Calculate and Report
        private void btnReport_Click(object sender, RoutedEventArgs e)
        {
            switch (oCurrentObject.GetType().Name)
            {
                case "ucAirConditioning":
                    ReportAir();
                    break;
                case "ucMainStorageCold":
                    ReportCold();
                    break;
                case "ucCoolAirR22":
                    ReportCoolAirR22();
                    break;
                case "ucEvaporaterR717":
                    ReportEvaporaterR717();
                    break;
                case "ucHorizontalPipeR12":
                    ReportHorizontalPipeR12();
                    break;
                case "ucSprinklerR717":
                    ReportSprinklerR717();
                    break;
                case "ucVerticalPipeR717":
                    ReportVerticalPipeR717();
                    break;

                case "ucCasingPipeSprinklers":
                    ReportCasingPipeSprinklers();
                    break;
                case "ucCasingPipeWing":
                    ReportCasingPipeWing();
                    break;
                case "ucCoolerWet":
                    ReportCoolerWet();
                    break;
                case "ucPlateAmoniac":
                    ReportPlateAmoniac();
                    break;
                case "SmoothPipeR717R22":
                    ReportSmoothPiper();
                    break;

                case "ucCoolingTower":
                    ReportCoolingTower();
                    break;
            }
        }

        private void ReportAir()
        {
            MainAir.SaveTab();
            SelectAlternative();
        }

        public void SelectAlternative()
        {
            Report.CalculateReport cr = new Report.CalculateReport();
            if (cr.LoadAlternative(MainAir.altman.lAlternative))
            {
                cr.ShowDialog();
            }
            else
            {
                MessageBox.Show("Calculation is not ready!");
            }
        }

        private void ReportCold()
        {
            Thread thread = new Thread(new ThreadStart(startloading));
            thread.SetApartmentState(ApartmentState.STA);
            thread.IsBackground = true;
            thread.Start();

            MainCold.SaveTab();
            if (MainCold.cColdStorage.lRoom.Count != 0)
            {
                MainCold.cColdStorage.CalculateImplement();
                Report.MainReport mr = new Report.MainReport();
                mr.LoadCrystalCold(MainCold.cColdStorage);
                foreach (ColdStorage.DOL.Room r in MainCold.cColdStorage.lRoom)
                {
                    mr.LoadTreeView(r);
                }
                thread.Abort();
                mr.ShowDialog();
            }
            else
            {
                thread.Abort();
                MessageBox.Show("Calculation is not ready!");
            }
        }

        #region Condenser
        private void ReportCoolAirR22()
        {
            MainCoolAirR22.coolAirR22ViewModel.ImplementCalculationCoolAirR22();
            Report.MainReport mr = new Report.MainReport();
            mr.LoadReportCoolAirR22(MainCoolAirR22.coolAirR22ViewModel.oCoolAirR22Result);
            mr.ShowDialog();
        }

        private void ReportEvaporaterR717()
        {
            MainEvaporaterR717.evaporaterR717ViewModel.ImplementCalculationEvaporaterR717();
            Report.MainReport mr = new Report.MainReport();
            mr.LoadReportEvaporaterR717(MainEvaporaterR717.evaporaterR717ViewModel.oEvaporaterR717Result);
            mr.ShowDialog();
        }

        private void ReportHorizontalPipeR12()
        {
            MainHorizontalPipeR12.horizontalPipeR12ViewModel.ImplementCalculationHorizontalPipeR12();
            Report.MainReport mr = new Report.MainReport();
            mr.LoadReportHorizontalPipeR12(MainHorizontalPipeR12.horizontalPipeR12ViewModel.oHorizontalPipeR12Result);
            mr.ShowDialog();
        }

        private void ReportSprinklerR717()
        {
            MainSprinklerR717.sprinklerR717ViewModel.ImplementCalculationSprinklerR717();
            Report.MainReport mr = new Report.MainReport();
            mr.LoadReportSprinklerR717(MainSprinklerR717.sprinklerR717ViewModel.oSprinklerR717Result);
            mr.ShowDialog();
        }

        private void ReportVerticalPipeR717()
        {
            MainVerticalPipeR717.verticalPipeR717ViewModel.ImplementCalculationVerticalPipeR717();
            Report.MainReport mr = new Report.MainReport();
            mr.LoadReportVerticalPipeR717(MainVerticalPipeR717.verticalPipeR717ViewModel.oVerticalPipeR717Result);
            mr.ShowDialog();
        }
        #endregion

        #region Evaporater
        private void ReportCasingPipeSprinklers()
        {
            MainCasingPipeSprinklers.CasingPipeSprinklersViewModel.ImplementCalculation();
            Report.MainReport mr = new Report.MainReport();
            mr.LoadReportCasingPipeSprinklers(MainCasingPipeSprinklers.CasingPipeSprinklersViewModel.oCasingPipeSprinklersResult);
            mr.ShowDialog();
        }

        private void ReportCasingPipeWing()
        {
            MainCasingPipeWing.CasingPipeWingViewModel.ImplementCalculation();
            Report.MainReport mr = new Report.MainReport();
            mr.LoadReportCasingPipeWing(MainCasingPipeWing.CasingPipeWingViewModel.oCasingPipeWingResult);
            mr.ShowDialog();
        }

        private void ReportCoolerWet()
        {
            MainCoolerWet.CoolerWetViewModel.ImplementCalculation();
            Report.MainReport mr = new Report.MainReport();
            mr.LoadReportCoolerWet(MainCoolerWet.CoolerWetViewModel.oCoolerWetResult);
            mr.ShowDialog();
        }

        private void ReportPlateAmoniac()
        {
            MainPlateAmoniac.PlateAmoniacViewModel.ImplementCalculation();
            Report.MainReport mr = new Report.MainReport();
            mr.LoadReportPlateAmoniac(MainPlateAmoniac.PlateAmoniacViewModel.oPlateAmoniacResult);
            mr.ShowDialog();
        }

        private void ReportSmoothPiper()
        {
            MainSmoothPipeR717R22.SmoothPiperR717R22ViewModel.ImplementCalculation();
            Report.MainReport mr = new Report.MainReport();
            mr.LoadReportSmoothPiperR717R22(MainSmoothPipeR717R22.SmoothPiperR717R22ViewModel.oSmoothPipeR717R22Result);
            mr.ShowDialog();
        }

        #endregion

        #region Cooling tower
        private void ReportCoolingTower()
        {
            MainCooling.CoolingTowerViewModel.ImplementCalculation();
            Report.MainReport mr = new Report.MainReport();
            mr.LoadReportCoolingTower(MainCooling.CoolingTowerViewModel.oResult);
            mr.ShowDialog();
        }
        #endregion

        #endregion
        #endregion

        #region Start Button
        private void btnAir_Click(object sender, RoutedEventArgs e)
        {
            AirClick();
        }

        private void AirClick()
        {
            try
            {
                oCurrentObject = new ucAirConditioning();
                iHome.Visibility = Visibility.Visible;
                btnLibrary.Visibility = Visibility.Visible;
                mSave.Visibility = Visibility.Visible;

                MainAir = new ucAirConditioning();
                temma = new TemplateManagement();
                temma.MyTemplate = new TemplateManagement.LoadTemplate(loadTemplate);
                temma.SendTemplate();
                ucListInformation1 = new Mainform.ucListInformation();
                this.pnlTreeView.Children.Clear();
                this.pnlTreeView.Children.Add(ucListInformation1);
                ucListInformation1.MyRoom = new Mainform.ucListInformation.GetRoom(GetRoom);
                ucListInformation1.MyAlt = new Mainform.ucListInformation.GetAlt(GetAlt);
                ucListInformation1.MyLocation = new Mainform.ucListInformation.GetLocation(GetLocation);
                ucListInformation1.NewAlt = new Mainform.ucListInformation.AddAlt(AddAlt);
                ucListInformation1.dAlt = new Mainform.ucListInformation.DeleteAlt(DeleteAlt);
                ucListInformation1.addAir();
                MainAir.MyRoom = new ucAirConditioning.AddRoom(AddRoom);
                MainAir.DelRoom = new ucAirConditioning.DeleteRoom(DeleteRoom);
                this.pnlMainGui.Children.Clear();
                this.pnlMainGui.Children.Add(MainAir);
                ucListInformation1.Visibility = Visibility.Visible;
                btnTemplate.Visibility = Visibility.Visible;
                btnReport.Visibility = Visibility.Visible;
            }
            catch (Exception r)
            {
                MessageBox.Show(r.ToString());
            }
        }

        private void btnCold_Click(object sender, RoutedEventArgs e)
        {
            ColdClick();
        }

        private void ColdClick()
        {
            try
            {
                iHome.Visibility = Visibility.Visible;
                btnLibrary.Visibility = Visibility.Visible;
                MainCold = new ucMainStorageCold();
                oCurrentObject = new ucMainStorageCold();
                mSave.Visibility = Visibility.Visible;

                ucListInformation1 = new Mainform.ucListInformation();
                this.pnlTreeView.Children.Clear();
                this.pnlTreeView.Children.Add(ucListInformation1);
                if (temma != null)
                {
                    temma.Close();
                }
                ucListInformation1.MyLocation = new Mainform.ucListInformation.GetLocation(GetLocation);
                ucListInformation1.NewCold = new Mainform.ucListInformation.AddCold(GetCold);
                MainCold.myRoomCold = new ucMainStorageCold.GetRoomCold(AddRoom);
                ucListInformation1.MyRoom = new Mainform.ucListInformation.GetRoom(GetRoom);
                MainCold.DelRoom = new ucMainStorageCold.DeleteRoom(DeleteRoom);
                btnTemplate.Visibility = Visibility.Hidden;
                ucListInformation1.addColdStorage();
                this.pnlMainGui.Children.Clear();
                this.pnlMainGui.Children.Add(MainCold);
                ucListInformation1.Visibility = Visibility.Visible;
                btnReport.Visibility = Visibility.Visible;


            }
            catch (Exception r)
            {
                MessageBox.Show(r.ToString());
            }
        }

        //  Condenser
        private void CondenserClick(int i)
        {
            // code content MainEvaR717
            try
            {
                this.pnlMainGui.Children.Clear();
                ucLeftEquimentDesign ucLeftEquimentDesign = new Mainform.ucLeftEquimentDesign();
                this.pnlTreeView.Children.Clear();
                this.pnlTreeView.Children.Add(ucLeftEquimentDesign);
                btnReport.Visibility = Visibility.Visible;
                btnLibrary.Visibility = Visibility.Visible;
                mSave.Visibility = Visibility.Visible;
                switch (i)
                {
                    case 0:
                        MainEvaporaterR717 = new ucEvaporaterR717();
                        this.pnlMainGui.Children.Add(MainEvaporaterR717);
                        oCurrentObject = new ucEvaporaterR717();
                        break;
                    case 1:
                        MainCoolAirR22 = new ucCoolAirR22();
                        this.pnlMainGui.Children.Add(MainCoolAirR22);
                        oCurrentObject = new ucCoolAirR22();
                        break;
                    case 2:
                        MainHorizontalPipeR12 = new ucHorizontalPipeR12();
                        this.pnlMainGui.Children.Add(MainHorizontalPipeR12);
                        oCurrentObject = new ucHorizontalPipeR12();
                        break;
                    case 3:
                        MainSprinklerR717 = new ucSprinklerR717();
                        this.pnlMainGui.Children.Add(MainSprinklerR717);
                        oCurrentObject = new ucSprinklerR717();
                        break;
                    case 4:
                        MainVerticalPipeR717 = new ucVerticalPipeR717();
                        this.pnlMainGui.Children.Add(MainVerticalPipeR717);
                        oCurrentObject = new ucVerticalPipeR717();
                        break;
                    default:
                        MainEvaporaterR717 = new ucEvaporaterR717();
                        this.pnlMainGui.Children.Add(MainEvaporaterR717);
                        oCurrentObject = new ucEvaporaterR717();
                        break;
                }
            }
            catch (Exception r)
            {
                MessageBox.Show(r.ToString());
            }
        }

        //  Evaporator
        private void EvaporatorClick(int i)
        {
            try
            {
                this.pnlMainGui.Children.Clear();
                ucLeftEquimentDesign ucLeftEquimentDesign = new Mainform.ucLeftEquimentDesign();
                this.pnlTreeView.Children.Clear();
                this.pnlTreeView.Children.Add(ucLeftEquimentDesign);
                btnReport.Visibility = Visibility.Visible;
                btnLibrary.Visibility = Visibility.Visible;
                mSave.Visibility = Visibility.Visible;
                switch (i)
                {
                    case 0:
                        MainCasingPipeSprinklers = new ucCasingPipeSprinklers();
                        this.pnlMainGui.Children.Add(MainCasingPipeSprinklers);
                        oCurrentObject = new ucCasingPipeSprinklers();
                        break;
                    case 1:
                        MainCasingPipeWing = new ucCasingPipeWing();
                        this.pnlMainGui.Children.Add(MainCasingPipeWing);
                        oCurrentObject = new ucCasingPipeWing();
                        break;
                    case 2:
                        MainCoolerWet = new ucCoolerWet();
                        this.pnlMainGui.Children.Add(MainCoolerWet);
                        oCurrentObject = new ucCoolerWet();
                        break;
                    case 3:
                        MainPlateAmoniac = new ucPlateAmoniac();
                        this.pnlMainGui.Children.Add(MainPlateAmoniac);
                        oCurrentObject = new ucPlateAmoniac();
                        break;
                    case 4:
                        MainSmoothPipeR717R22 = new SmoothPipeR717R22();
                        this.pnlMainGui.Children.Add(MainSmoothPipeR717R22);
                        oCurrentObject = new SmoothPipeR717R22();
                        break;
                    default:
                        MainCasingPipeSprinklers = new ucCasingPipeSprinklers();
                        this.pnlMainGui.Children.Add(MainCasingPipeSprinklers);
                        oCurrentObject = new ucCasingPipeSprinklers();
                        break;
                }
            }
            catch (Exception r)
            {
                MessageBox.Show(r.ToString());
            }

        }

        //  Cooling Tower
        private void CoolingClick(int i)
        {
            try
            {
                this.pnlMainGui.Children.Clear();
                ucLeftEquimentDesign ucLeftEquimentDesign = new Mainform.ucLeftEquimentDesign();
                this.pnlTreeView.Children.Clear();
                this.pnlTreeView.Children.Add(ucLeftEquimentDesign);
                btnReport.Visibility = Visibility.Visible;
                btnLibrary.Visibility = Visibility.Visible;
                mSave.Visibility = Visibility.Visible;
                switch (i)
                {
                    case 0:
                        MainCooling = new ucCoolingTower();
                        this.pnlMainGui.Children.Add(MainCooling);
                        oCurrentObject = new ucCoolingTower();
                        break;
                    default:

                        MainCooling = new ucCoolingTower();
                        this.pnlMainGui.Children.Add(MainCooling);
                        oCurrentObject = new ucCoolingTower();
                        break;

                }
            }
            catch (Exception r)
            {
                MessageBox.Show(r.ToString());
            }
        }
        #endregion
        #endregion

        #region Save Project
        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.Filter = "Eco File|*.eco|XML File|*.xml";
            saveFileDialog1.Title = "Save Project";
            saveFileDialog1.ShowDialog();
            if (saveFileDialog1.FileName != "")
            {
                string FileName = saveFileDialog1.FileName;

                XmlTextWriter tw = new XmlTextWriter(FileName, Encoding.UTF8);

                tw.Formatting = Formatting.Indented;
                tw.Indentation = 3;

                tw.WriteStartDocument();
                switch (oCurrentObject.GetType().Name)
                {
                    case "ucAirConditioning":
                        MainAir.SaveProject(tw);
                        break;
                    case "ucMainStorageCold":
                        MainCold.SaveProject(tw);
                        break;
                    case "ucCoolAirR22":
                        SaveEquipment("Condenser", tw, MainCoolAirR22.coolAirR22ViewModel.oCoolAirR22);
                        break;
                    case "ucEvaporaterR717":
                        SaveEquipment("Condenser", tw, MainEvaporaterR717.evaporaterR717ViewModel.oEvaporaterR717);
                        break;
                    case "ucHorizontalPipeR12":
                        SaveEquipment("Condenser", tw, MainHorizontalPipeR12.horizontalPipeR12ViewModel.oHorizontalPipeR12);
                        break;
                    case "ucSprinklerR717":
                        SaveEquipment("Condenser", tw, MainSprinklerR717.sprinklerR717ViewModel.oSprinklerR717);
                        break;
                    case "ucVerticalPipeR717":
                        SaveEquipment("Condenser", tw, MainVerticalPipeR717.verticalPipeR717ViewModel.oVerticalPipeR717);
                        break;

                    case "ucCasingPipeSprinklers":
                        SaveEquipment("Evaporator", tw, MainCasingPipeSprinklers.CasingPipeSprinklersViewModel.oSprinlers);
                        break;
                    case "ucCasingPipeWing":
                        SaveEquipment("Evaporator", tw, MainCasingPipeWing.CasingPipeWingViewModel.oCasingPipeWing);
                        break;
                    case "ucCoolerWet":
                        SaveEquipment("Evaporator", tw, MainCoolerWet.CoolerWetViewModel.oCoolerWet);
                        break;
                    case "ucPlateAmoniac":
                        SaveEquipment("Evaporator", tw, MainPlateAmoniac.PlateAmoniacViewModel.oPlateAmoniac);
                        break;
                    case "SmoothPipeR717R22":
                        SaveEquipment("Evaporator", tw, MainSmoothPipeR717R22.SmoothPiperR717R22ViewModel.oSmoothPipeR717R22);
                        break;

                    case "ucCoolingTower":
                        SaveEquipment("CoolingTower", tw, MainCooling.CoolingTowerViewModel.ocoolingTower);
                        break;
                }
                tw.WriteEndDocument();

                tw.Close();

                MessageBox.Show("Successful!");
            }
        }

        private void SaveEquipment(String sType, XmlTextWriter tw, object obj)
        {
            tw.WriteStartElement(sType);
            tw.WriteStartElement(obj.GetType().Name);
            foreach (PropertyInfo property in obj.GetType().GetProperties())
            {
                if (property.GetValue(obj, null) != null)
                {
                    tw.WriteAttributeString(property.Name, property.GetValue(obj, null).ToString());
                }
                else
                {
                    tw.WriteAttributeString(property.Name, "");
                }
            }
            tw.WriteEndElement();
            tw.WriteEndElement();
        }
        #endregion

        #region Open Project
        private void OpenButton_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Filter = "Eco Files (*.ECO)|*.ECO|All files (*.*)|*.*";
            openFileDialog1.RestoreDirectory = true;

            if (openFileDialog1.ShowDialog().Value)
            {
                XmlTextReader xtr = new XmlTextReader(openFileDialog1.FileName);
                while (xtr.Read())
                {
                    if (xtr.NodeType == XmlNodeType.Element)
                    {
                        switch (xtr.Name)
                        {
                            case "AirCondition":
                                AirClick();
                                OpenAirCondition(xtr);
                                break;
                            case "ColdStorage":
                                ColdClick();
                                int iCold = 0;
                                foreach (PropertyInfo property in MainCold.cColdStorage.GetType().GetProperties())
                                {
                                    CheckProperty(property, iCold, MainCold.cColdStorage, xtr);
                                    iCold++;
                                }                                
                                OpenColdStorage(xtr);
                                break;
                            case "Condenser":
                                OpenCondenser(xtr);
                                break;
                            case "Evaporator":
                                OpenEvaporator(xtr);
                                break;
                            case "CoolingTower":
                                OpenCoolingTower(xtr);
                                break;
                        }
                        break;
                    }
                }
            }
        }

        private void OpenAirCondition(XmlTextReader xtr)
        {
            while (xtr.Read())
            {
                if (xtr.NodeType == XmlNodeType.Element)
                {
                    switch (xtr.Name)
                    {
                        case "Alternative":
                            int iAlter = 0;
                            Alternative alt = new Alternative();
                            foreach (PropertyInfo property in alt.GetType().GetProperties())
                            {
                                CheckProperty(property, iAlter, alt, xtr);
                                iAlter++;
                            }
                            MainAir.addAlt(alt);
                            ucListInformation1.LoadAlternative(alt);
                            MainAir.loadAlt((Alternative)ucListInformation1.tvAlt.Tag);
                            break;
                        case "Room":
                            int iRoom = 0;
                            Air_condition.DO.Room room = new Air_condition.DO.Room();
                            foreach (PropertyInfo property in room.GetType().GetProperties())
                            {
                                CheckProperty(property, iRoom, room, xtr);
                                iRoom++;
                            }
                            MainAir.addRoom(room);
                            MainAir.loadRoom((Air_condition.DO.Room)ucListInformation1.tvRoom.Tag);
                            MainAir.loadAlt((Alternative)ucListInformation1.tvAlt.Tag);
                            break;
                        case "InternalLoad":
                            int iInter = 0;
                            Air_condition.DO.InternalLoad inter = new Air_condition.DO.InternalLoad();
                            foreach (PropertyInfo property in inter.GetType().GetProperties())
                            {
                                CheckProperty(property, iInter, inter, xtr);
                                iInter++;
                            }
                            MainAir.LoadObject(inter);
                            break;
                        case "Equipment":
                            int iequip = 0;
                            Equip equip = new Equip();
                            foreach (PropertyInfo property in equip.GetType().GetProperties())
                            {
                                CheckProperty(property, iequip, equip, xtr);
                                iequip++;
                            }
                            MainAir.LoadObject(equip);
                            break;
                        case "Glass":
                            int iGlass = 0;
                            Glass glass = new Glass();
                            foreach (PropertyInfo property in glass.GetType().GetProperties())
                            {
                                CheckProperty(property, iGlass, glass, xtr);
                                iGlass++;
                            }
                            MainAir.LoadObject(glass);
                            break;
                        case "Roof":
                            int iroof = 0;
                            Air_condition.DO.Roof roof = new Air_condition.DO.Roof();
                            foreach (PropertyInfo property in roof.GetType().GetProperties())
                            {
                                CheckProperty(property, iroof, roof, xtr);
                                iroof++;
                            }
                            MainAir.LoadObject(roof);
                            break;
                        case "Wall":
                            int iwall = 0;
                            Air_condition.DO.Wall wall = new Air_condition.DO.Wall();
                            foreach (PropertyInfo property in wall.GetType().GetProperties())
                            {
                                CheckProperty(property, iwall, wall, xtr);
                                iwall++;
                            }
                            MainAir.LoadObject(wall);
                            break;
                        case "Floor":
                            int ifloor = 0;
                            Air_condition.DO.Floor floor = new Air_condition.DO.Floor();
                            foreach (PropertyInfo property in floor.GetType().GetProperties())
                            {
                                CheckProperty(property, ifloor, floor, xtr);
                                ifloor++;
                            }
                            MainAir.LoadObject(floor);
                            break;
                        case "Ceiling":
                            int iceiling = 0;
                            Ceiling ceiling = new Ceiling();
                            foreach (PropertyInfo property in ceiling.GetType().GetProperties())
                            {
                                CheckProperty(property, iceiling, ceiling, xtr);
                                iceiling++;
                            }
                            MainAir.LoadObject(ceiling);
                            break;
                    }
                }
            }
        }

        private void OpenColdStorage(XmlTextReader xtr)
        {
            while (xtr.Read())
            {
                if (xtr.NodeType == XmlNodeType.Element)
                {
                    switch (xtr.Name)
                    {
                        case "Room":
                            int iRoom = 0;
                            ColdStorage.DOL.Room room = new ColdStorage.DOL.Room();
                            foreach (PropertyInfo property in room.GetType().GetProperties())
                            {
                                CheckProperty(property, iRoom, room, xtr);
                                iRoom++;
                            }
                            MainCold.addRoom(room, false);
                            MainCold.loadroom((ColdStorage.DOL.Room)ucListInformation1.tvRoom.Tag);
                            break;
                        case "Product":
                            int iProduct = 0;
                            ColdStorage.DOL.Product product = new ColdStorage.DOL.Product();
                            foreach (PropertyInfo property in product.GetType().GetProperties())
                            {
                                CheckProperty(property, iProduct, product, xtr);
                                iProduct++;
                            }
                            MainCold.LoadObject(product);
                            break;
                        case "InternalLoad":
                            int iInternal = 0;
                            ColdStorage.DOL.InternalLoad inter = new ColdStorage.DOL.InternalLoad();
                            foreach (PropertyInfo property in inter.GetType().GetProperties())
                            {
                                CheckProperty(property, iInternal, inter, xtr);
                                iInternal++;
                            }
                            MainCold.LoadObject(inter);
                            break;
                        case "Floor":
                            int iFloor = 0;
                            ColdStorage.DOL.Floor floor = new ColdStorage.DOL.Floor();
                            foreach (PropertyInfo property in floor.GetType().GetProperties())
                            {
                                CheckProperty(property, iFloor, floor, xtr);
                                iFloor++;
                            }
                            MainCold.LoadObject(floor);
                            break;
                        case "Wall":
                            int iWall = 0;
                            ColdStorage.DOL.Wall wall = new ColdStorage.DOL.Wall();
                            foreach (PropertyInfo property in wall.GetType().GetProperties())
                            {
                                CheckProperty(property, iWall, wall, xtr);
                                iWall++;
                            }
                            MainCold.LoadObject(wall);
                            break;
                        case "Roof":
                            int iRoof = 0;
                            ColdStorage.DOL.Roof roof = new ColdStorage.DOL.Roof();
                            foreach (PropertyInfo property in roof.GetType().GetProperties())
                            {
                                CheckProperty(property, iRoof, roof, xtr);
                                iRoof++;
                            }
                            MainCold.LoadObject(roof);
                            break;
                    }
                }
            }
        }

        private void OpenCondenser(XmlTextReader xtr)
        {
            int iCondenser = 0;
            while (xtr.Read())
            {
                if (xtr.NodeType == XmlNodeType.Element)
                {
                    switch (xtr.Name)
                    {
                        case "EvaporaterR717":
                            CondenserClick(0);
                            foreach (PropertyInfo property in MainEvaporaterR717.evaporaterR717ViewModel.oEvaporaterR717.GetType().GetProperties())
                            {
                                CheckProperty(property, iCondenser, MainEvaporaterR717.evaporaterR717ViewModel.oEvaporaterR717, xtr);
                                iCondenser++;
                            }
                            break;
                        case "CoolAirR22":
                            CondenserClick(1);
                            foreach (PropertyInfo property in MainCoolAirR22.coolAirR22ViewModel.oCoolAirR22.GetType().GetProperties())
                            {
                                CheckProperty(property, iCondenser, MainCoolAirR22.coolAirR22ViewModel.oCoolAirR22, xtr);
                                iCondenser++;
                            }
                            break;
                        case "HorizontalPipeR12":
                            CondenserClick(2);
                            foreach (PropertyInfo property in MainHorizontalPipeR12.horizontalPipeR12ViewModel.oHorizontalPipeR12.GetType().GetProperties())
                            {
                                CheckProperty(property, iCondenser, MainHorizontalPipeR12.horizontalPipeR12ViewModel.oHorizontalPipeR12, xtr);
                                iCondenser++;
                            }
                            break;
                        case "SprinklerR717":
                            CondenserClick(3);
                            foreach (PropertyInfo property in MainSprinklerR717.sprinklerR717ViewModel.oSprinklerR717.GetType().GetProperties())
                            {
                                CheckProperty(property, iCondenser, MainSprinklerR717.sprinklerR717ViewModel.oSprinklerR717, xtr);
                                iCondenser++;
                            }
                            break;
                        case "VerticalPipeR717":
                            CondenserClick(4);
                            foreach (PropertyInfo property in MainVerticalPipeR717.verticalPipeR717ViewModel.oVerticalPipeR717.GetType().GetProperties())
                            {
                                CheckProperty(property, iCondenser, MainVerticalPipeR717.verticalPipeR717ViewModel.oVerticalPipeR717, xtr);
                                iCondenser++;
                            }
                            break;
                    }
                }
            }
        }

        private void OpenEvaporator(XmlTextReader xtr)
        {
            int iEvaporator = 0;
            while (xtr.Read())
            {
                if (xtr.NodeType == XmlNodeType.Element)
                {
                    switch (xtr.Name)
                    {
                        case "CasingPipeSprinklers":
                            EvaporatorClick(0);
                            foreach (PropertyInfo property in MainCasingPipeSprinklers.CasingPipeSprinklersViewModel.oSprinlers.GetType().GetProperties())
                            {
                                CheckProperty(property, iEvaporator, MainCasingPipeSprinklers.CasingPipeSprinklersViewModel.oSprinlers, xtr);
                                iEvaporator++;
                            }
                            break;
                        case "CasingPipeWing":
                            EvaporatorClick(1);
                            foreach (PropertyInfo property in MainCasingPipeWing.CasingPipeWingViewModel.oCasingPipeWing.GetType().GetProperties())
                            {
                                CheckProperty(property, iEvaporator, MainCasingPipeWing.CasingPipeWingViewModel.oCasingPipeWing, xtr);
                                iEvaporator++;
                            }
                            break;
                        case "CoolerWet":
                            EvaporatorClick(2);
                            foreach (PropertyInfo property in MainCoolerWet.CoolerWetViewModel.oCoolerWet.GetType().GetProperties())
                            {
                                CheckProperty(property, iEvaporator, MainCoolerWet.CoolerWetViewModel.oCoolerWet, xtr);
                                iEvaporator++;
                            }
                            break;
                        case "PlateAmoniac":
                            EvaporatorClick(3);
                            foreach (PropertyInfo property in MainPlateAmoniac.PlateAmoniacViewModel.oPlateAmoniac.GetType().GetProperties())
                            {
                                CheckProperty(property, iEvaporator, MainPlateAmoniac.PlateAmoniacViewModel.oPlateAmoniac, xtr);
                                iEvaporator++;
                            }
                            break;
                        case "SmoothPipeR717R22":
                            EvaporatorClick(4);
                            foreach (PropertyInfo property in MainSmoothPipeR717R22.SmoothPiperR717R22ViewModel.oSmoothPipeR717R22.GetType().GetProperties())
                            {
                                CheckProperty(property, iEvaporator, MainSmoothPipeR717R22.SmoothPiperR717R22ViewModel.oSmoothPipeR717R22, xtr);
                                iEvaporator++;
                            }
                            break;
                    }
                }
            }
        }

        private void OpenCoolingTower(XmlTextReader xtr)
        {
            int iCooling = 0;
            while (xtr.Read())
            {
                if (xtr.NodeType == XmlNodeType.Element)
                {
                    switch (xtr.Name)
                    {
                        case "CoolingTower":
                            CoolingClick(0);
                            foreach (PropertyInfo property in MainCooling.CoolingTowerViewModel.ocoolingTower.GetType().GetProperties())
                            {
                                CheckProperty(property, iCooling, MainCooling.CoolingTowerViewModel.ocoolingTower, xtr);
                                iCooling++;
                            }
                            break;
                    }
                }
            }
        }

        private void CheckProperty(PropertyInfo property, int i, object obj, XmlTextReader xtr)
        {
            switch (property.PropertyType.ToString())
            {
                case "System.Int32":
                    property.SetValue(obj, Convert.ToInt32(xtr.GetAttribute(i)), null);
                    break;
                case "System.Double":
                    property.SetValue(obj, Convert.ToDouble(xtr.GetAttribute(i)), null);
                    break;
                case "System.String":
                    property.SetValue(obj, xtr.GetAttribute(i), null);
                    break;
            }
        }
        #endregion

        #region Close Project
        private void Ecosystem_Closed(object sender, EventArgs e)
        {
            Application.Current.Shutdown();
        }
        #endregion

        #region Process Searching
        private void Image_MouseDown(object sender, MouseButtonEventArgs e)
        {
            System.Windows.Forms.Help.ShowHelp(null, ReferencePath.ReferenceFile, System.Windows.Forms.HelpNavigator.KeywordIndex, txtSearch.Text);
        }

        private void txtSearch_GotFocus(object sender, RoutedEventArgs e)
        {
            txtSearch.Text = "";
        }

        private void txtSearch_LostFocus(object sender, RoutedEventArgs e)
        {
            txtSearch.Text = "Search...";
        }

        private void txtSearch_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                System.Windows.Forms.Help.ShowHelp(null, ReferencePath.ReferenceFile, System.Windows.Forms.HelpNavigator.KeywordIndex, txtSearch.Text);
            }
        }
        #endregion

    }
}
